/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.ks.client.components;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import org.macdadi.core.client.domain.Goal;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class GoalDetail extends Composite
{
    protected Goal goal;

    private final VerticalPanel container;

    private FlexTable table;    
    private Label nameLabel, idLabel;
    private Button editButton, saveButton;
    private TextBox newNameInput;

    public GoalDetail()
    {
        container = new VerticalPanel();

        initWidget(container);
        setStyleName("macdadi-GoalDetail");
        table = new FlexTable(); // 5x2
        container.add(table);

        table.setHTML(0, 0, "<b>Goal Detail</b>");
        ((FlexTable.FlexCellFormatter) table.getCellFormatter()).setColSpan(0, 0, 2);

        table.setText(1, 0, "Name:");
        nameLabel = new Label();
        table.setWidget(1, 1, nameLabel);

        table.setText(2, 0, "ID:");
        idLabel = new Label();
        table.setWidget(2, 1, idLabel);

        // TODO: why do these buttons get so wide?
        editButton = new Button("Edit");
        table.setWidget(3, 0, editButton);
        saveButton = new Button("Save");
        table.setWidget(3, 1, saveButton);

        table.setText(4, 0, "New name:");
        newNameInput = new TextBox();
        table.setWidget(4, 1, newNameInput);
        
        setEditMode(false);
        setGoal(null);

        editButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender)
            {
                setEditMode(true);
            }
        });

        saveButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender)
            {
                if ( goal != null ) {
                    String newName = newNameInput.getText().trim();
                    if ( newName.length() == 0 ) {
                        Window.alert("Name may not be blank");
                        return;
                    }

                    if ( ! newName.equals(goal.getName())) {
                        // TODO: update the database, then refresh the tree (how? fire an event?)
                        Window.alert("Should rename goal #" + goal.getId() + " to: " + newName);
                    }
                }

                setEditMode(false);
            }
        });
    }

    protected void setEditMode(boolean doEdit) {
        newNameInput.setText("");
        table.getRowFormatter().setVisible(4, doEdit);
        saveButton.setVisible(doEdit);
    }

    public void setGoal(Goal goal)
    {
        this.goal = goal;

        setEditMode(false);

        if ( goal == null ) {
            table.setVisible(false); // the rest may not be necessary if we do this
            nameLabel.setText("");
            idLabel.setText("");
            editButton.setVisible(false);
            saveButton.setVisible(false);
        } else {
            table.setVisible(true);
            nameLabel.setText(goal.getName());
            idLabel.setText(Integer.toString(goal.getId()));
            editButton.setVisible(true);
            saveButton.setVisible(false);
        }
    }
}
